import { userInfoAPI } from '@/api'
import { setToken, getToken, delToken } from '@/utils/auth'

const state = {
  token: getToken() || null,
  userInfo: null
}

const mutations = {
  // 保存token
  saveToken (state, payload) {
    state.token = payload
    setToken(payload)
  },
  // 保存用户信息
  saveUserInfo (state, payload) {
    state.userInfo = payload
  },
  // 删除token和用户信息
  logout (state) {
    state.token = {}
    state.userInfo = {}
    delToken()
  }
}
const actions = {
  // 获取用户信息
  async getUserInfo (context, payload) {
    const res1 = await userInfoAPI()
    // console.log('res1:', res1)
    // 保存用户信息到vuex
    context.commit('saveUserInfo', res1.data.data)
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}
